struct HashTable {  
    buckets: Vec<Option<Vec<(String, i32)>>>,  
}  

impl HashTable {  
    // 构造函数初始化哈希表
    fn new(capacity: usize) -> Self {
        let mut buckets = Vec::with_capacity(capacity);
        for _ in 0..capacity {
            buckets.push(Some(Vec::new())); // 初始化每个桶为包含空向量的Some
        }
        HashTable { buckets }
    }

    // 简单哈希函数实现
    fn hash(&self, key: &str) -> usize {
        let mut sum = 0;
        for c in key.chars() {
            sum += c as usize;
        }
        sum % self.buckets.len()
    }

    // 插入键值对（含冲突处理）
    fn insert(&mut self, key: String, value: i32) {  
        let index = self.hash(&key);  
        // TODO: 处理冲突（链地址法）  
        if let Some(bucket) = &mut self.buckets[index] {
            // 检查并更新现有键
            for (k, v) in bucket.iter_mut() {
                if *k == key {
                    *v = value;
                    return;
                }
            }
            // 添加新键值对
            bucket.push((key, value));
        }
    }  

    // 查找键值对
    fn get(&self, key: &str) -> Option<i32> {
        let index = self.hash(key);
        if let Some(bucket) = &self.buckets[index] {
            for (k, v) in bucket {
                if k == key {
                    return Some(*v);
                }
            }
        }
        None
    }
}  

#[test]  
fn test_collision() {  
    let mut table = HashTable::new(2);  
    table.insert("a".into(), 1);  
    table.insert("b".into(), 2);  
    assert_eq!(table.get("a"), Some(1));   // 验证冲突处理
}  

// 添加main函数
fn main() {
    let mut table = HashTable::new(5);
    table.insert("apple".into(), 10);
    table.insert("banana".into(), 20);
    println!("apple: {:?}", table.get("apple"));
    println!("banana: {:?}", table.get("banana"));
}